package com.saydc.forms.capacitaciones;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import com.saydc.dao.EmpleadosDAO;

public class EnviarCapacitacionForm extends ActionForm {

	private static final long serialVersionUID = 1L;
	private String legajo;
	private int capacitacion;
	public String getLegajo() {
		return legajo;
	}
	public void setLegajo(String legajo) {
		this.legajo = legajo;
	}
	public int getCapacitacion() {
		return capacitacion;
	}
	public void setCapacitacion(int capacitacion) {
		this.capacitacion = capacitacion;
	}
	
	@Override
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		if(legajo == null || legajo.length() != 8){
			errors.add("legajoRequerido", new ActionMessage("error.incorporacion.legajoInvalido"));
		}else if(existeLegajo(legajo) == false){
			errors.add("legajoRequerido", new ActionMessage("error.incorporacion.legajoNoRegistrado"));
		}
		if(capacitacion == -1){
			errors.add("capacitacionRequired", new ActionMessage("error.capacitacion.required"));
		}
		return errors;
	}
	
	private boolean existeLegajo(String legajo) {
		EmpleadosDAO dao = EmpleadosDAO.getInstance();
		if(dao.buscarEmpleadoPorLegajo(legajo) == null){
			return false;
		}
		return true;
	}

}
